package com.yunche.yunchebusiness.base

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * Create by Crcker on 2018/12/20 11:11
 * Email: Crcker@163.com
 */


open abstract class BaseFragment : Fragment() {
    open var rootView: View? = null


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (rootView == null) {
            rootView = inflater.inflate(setLayout(), null)
        }
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView()
        initData()
        super.onViewCreated(view, savedInstanceState)
    }

    //跳转Activity
    open fun openActivity(clazz: Class<*>) {
        startActivity(Intent(activity, clazz))
    }

    //携带数据 跳转
    open fun openActivityWithBundle(clz: Class<*>, bundle: Bundle?) {
        val intent = Intent(activity, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
    }

    //带返回值的跳转
    open fun openActivityForResult(clz: Class<*>, bundle: Bundle?, requestCode: Int) {
        val intent = Intent(activity, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }


    abstract fun setLayout(): Int

    abstract fun initView()

    abstract fun initData()
}